package ie.lawlor.amvc.example.explorer.variantlist;

import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.patterns.table.TableModel;
import tmp.AmazonProduct;
import tmp.AmazonVariant;


/**
 * @author blawlor
 *
 */
public class VariantListModel extends TableModel {
	
	public VariantListModel(String name){
		super(name);
		initFields();
	}
	private void initFields(){
		Field field = new Field(1, true);
		field.setTitle("SKU");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(2, true);
		field.setTitle("Title");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(3, true);
		field.setTitle("Active");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(4, true);
		field.setTitle("Variation");
		fields.add(field);
		visibleFields.add(field);
	}
	
	public void doList(Event e){
		fireUpdateView();
	}

  public void doChangeList(Event e){
    logger.debug("About to update the list to be " + e.getPayload());
    if (e.getPayload() instanceof AmazonProduct){
      AmazonProduct product = (AmazonProduct)e.getPayload();
      list = product.getVariants();
      fireUpdateView();
    }
  }  

	public int getMaxWidth(int columnIndex) {
		switch (columnIndex){
		   case 0: return 12; 
		   case 1: return 100; 
		   case 2: return 5;
		   case 3: return 15;
		   default: return 10;
		}
	}

	public int getPreferredWidth(int columnIndex) {
		switch (columnIndex){
		   case 0: return 12; 
		   case 1: return 25; 
		   case 2: return 5;
		   case 3: return 15;
		   default: return 10;
		}
	}

	public Object getValueAt(int x, int y) {
		AmazonVariant v = (AmazonVariant)list.get(x);
		switch (y){
		   case 0: return v.getSKU(); 
		   case 1: return v.getTitle(); 
		   case 2: return v.isActive()?"Y":"N";
		   case 3: return v.getVariation();
		   default: return null;
		}
	}
}
